#!/usr/bin/env python
import string

class demo:                            
    def __init__(self, mas="mas", mod="mod"): #Openfile
        try:
            self.logfiles = {};
            self.logfiles["masFile"] = open (mas,"rb")
            self.logfiles["modFile"] = open (mod,"rb")

         #Get start time
            _start_time1 = self.getOneMasLog()[0]
            _start_time2 = self.getOneModLog()[0]
            self.start_time = min(string.atoi(_start_time1),string.atoi(_start_time2))
        except IOError:
            print "Open file error."
            pass

    def getOneMasLog(self):
        return self.logfiles["masFile"].readline().split('|<:split:>|')
        
    def getOneModLog(self):
        return self.logfiles["modFile"].readline().split('|<:split:>|')
        
    def printMasLog(self, msg):  #Format and Output log message
        time = "%12.06f"%((string.atoi(msg[0]) - self.start_time)/1000000.0)
        print "\033[36;1m[%s][MODULE]%s\033[0m"%(time, msg[3]),
        #print title,time, msg

    def printModLog(self, msg):  #Format and Output log message
        time = "%12.06f"%((string.atoi(msg[0]) - self.start_time)/1000000.0)
        print "\033[35;1m[%s][MASTER]%s\033[0m"%(time, msg[3]),

    def assemble(self):    #assemble log record
        self.logfiles["masFile"].seek(0)
        self.logfiles["modFile"].seek(0)

        mas_log = self.getOneMasLog()
        mod_log = self.getOneModLog()

        while True:
            if len(mas_log)==1:
                while  len(mod_log)!=1 :
                    self.printModLog(mod_log)
                    mod_log = self.getOneModLog()
                break
            if len(mod_log)==1:
                while  len(mas_log)!=1 :
                    self.printMasLog(mas_log)
                    mas_log = self.getOneMasLog()
                break

            if (string.atoi(mas_log[0]) < string.atoi(mod_log[0])):
                self.printMasLog(mas_log)
                mas_log = self.getOneMasLog()
            else:
                self.printModLog(mod_log)
                mod_log = self.getOneModLog()

 
tef = demo("mastertomod", "modtomaster") 
tef.assemble()
